/**
 * 
 */
package cn.panda.edu.teacher;

import cn.panda.edu.model.Course;
import cn.panda.edu.model.Courseware;
import cn.panda.edu.service.CourseService;
import cn.panda.edu.service.CoursewareService;
import cn.panda.edu.util.Constant;
import cn.panda.edu.util.FileUtil;
import cn.panda.edu.util.ResultDataUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**  
 * 类描述：  
 */
@RestController
@RequestMapping("/api/teacher/courseware")
public class TeacherCoursewareController {
	//每页显示条数
	//final int PAGESIZE=10;


	@Autowired
	private CoursewareService coursewareService;
	@Autowired
	private CourseService courseService;
	
	@ApiOperation(value="获取课件列表" , notes="")//用以指明api名称 
	@ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "课件名字", required = false, dataType = "String"),
        @ApiImplicitParam(name = "pageIndex", value = "当前页数", required = true, dataType = "Integer")
	})
	@RequestMapping(value="/list",method= RequestMethod.POST)
	public Object getAllCourseware(String name,Integer pageIndex){
		if(pageIndex==null) {
			pageIndex=1;
		}
		PageInfo<Courseware> pageInfo=coursewareService.getAllCourseware(pageIndex,Constant.PAGE_SIZE,name);
		return ResultDataUtil.resData(200, "", pageInfo);
	}
	@ApiOperation(value="根据id删除课件" , notes="")
	@RequestMapping(value="/delete",method= RequestMethod.POST)
	public  Object deleteCourseware(Integer id) {
		int  status=200;
		String message=null;
		try {
			coursewareService.deleteByPrimaryKey(id);
			status=200;
			message= "删除成功!";
		} catch (Exception e) {
			status=500;
			message="删除失败!";
		}
		return ResultDataUtil.resData(status, message, null);
	}
	@ApiOperation(value="添加课件")
	@RequestMapping(value="/insert",method= RequestMethod.POST)
	public  Object insertCourseware(@RequestBody Courseware courseware,
                                    HttpServletRequest request) {
		int  status=200;
		String message=null;
		try {
			courseware.setCoursewareAddtime(new Date());
			coursewareService.insert(courseware);
			status=200;
			message="添加成功!";
		} catch (Exception e) {
			status=500;
			message="添加失败!";
		}
		return ResultDataUtil.resData(status, message, null);
	}
	@ApiOperation(value="上传课件")
	@RequestMapping(value="/upload",method= RequestMethod.POST)
	public  Object insertCourseware(@RequestParam("file") MultipartFile file,
            HttpServletRequest request) {
		
		int  status=200;
		String message=null;
        String filePath = null ;
        try {
            filePath = FileUtil.uploadFile(file) ;
        } catch (Exception e) {
            e.printStackTrace();
        }
		return ResultDataUtil.resData(status, message, filePath);
	}


	@RequestMapping(value="/getCourseList",method= RequestMethod.POST)
	public Object getCourseList(){
		List<Course> list=courseService.getCourseList();
		return ResultDataUtil.resData(200, "", list);
	}
}
